package edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.photo;

import java.util.List;

import com.androidquery.AQuery;

import edu.mtu.citizenscience.cs.notepad.activity.R;
import edu.mtu.citizenscience.cs.notepad.model.note.ModelImage;
import edu.mtu.citizenscience.cs.utils.Image;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Photo Grid Adapter adapts photo models into photo grid
 */
public class PhotoGridAdapter extends ArrayAdapter<ModelImage> {
	// android-query library
	private AQuery aq;

	/**
	 * Create the Photo Grid Adapter
	 * 
	 * @param aq - AQuery - reference to housing activities android query instance
	 * @param images - List<ModelImage> - list of images
	 * @return void
	 */
	public PhotoGridAdapter(AQuery aq, List<ModelImage> images)
	{
		// create array adapter with list photo layout and photo models
		super(aq.getContext(), R.layout.list_item_photo, images);
		
		// set the android query to instance from containing activity
		this.aq = aq;
	}

	/**
	 * Get the view for a Photo Grid photo
	 * 
	 * @param int - position - the views position in the list
	 * @param convertView - View - the view for the list item
	 * @param parent - ViewGroup - containing list view
	 * @return View - the populated view for the list item
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		// check if the view has not been inflated before
		if (convertView == null) 
	    {
			// Inflate the new view
	    	convertView = this.aq.inflate(convertView, R.layout.list_item_photo, parent);
	    }
		
		// get the grid photo image view and attach a the photo scaled to 180x180
		Image.scaleImage(this.getItem(position).getPath(), (ImageView) convertView.findViewById(R.id.photoGridImage), 180, 180);
		
		// return the grid item view
		return convertView;
	}
}
